<?php

namespace App\Http\Controllers\Auth;

use App\Models\User;
use Illuminate\Http\Request;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{
  /*  protected $redirectAfterLogout = '/admin/login';*/
    protected $loginPath = '/auth/login';
    /*protected $redirectTo = '/admin/index';*/
    protected $redirectPath = '/admin/index';
    protected $loginView = 'admin.auth.login';
    //protected $registerView = 'admin.auth.register';
    protected $maxLoginAttempts = 5; //每分钟最大尝试登录次数
    protected $lockoutTime = 600;  //登录锁定时间
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * 重写登录
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
     */
    public function login(Request $request)
    {
        $this->validateLogin($request);

        $credentials = $this->getCredentials($request);
        $email = $request->input('email');
        $passwd = $request->input('password');
        if(env('ADMIN_USER')==$email && md5($passwd)==env('ADMIN_USER_PASSWD')){
            session(['isAdmin' => 'true']);
            return redirect($this->redirectPath);
        }

        elseif(Auth::attempt($credentials)) {
            return redirect($this->redirectPath);
        }
        else{
            return redirect($this->loginPath)->withErrors('用户不存在或密码错误！');
        }

    }

    public function getLogout(Request $request){
        if($this->isAdmin()){
            session()->forget('isAdmin');
            return redirect('auth/login');
        }else{
            parent::getLogout();
        }
    }


    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

    /**
     * @param Request $request
     */
    protected function validateLogin(Request $request)
    {
        $this->validate($request,[
            $this->loginUsername() => 'required',
            'password' => 'required',
            'captcha' => 'required|captcha'
        ], [
            'email.required' => '用户名必填',
            'password.required' => '密码必填',
            'captcha.captcha' => '验证码错误',
            'captcha.required' => '验证码必填',
        ]);
    }


}
